<template>
  <!-- PAGE CONTENT BEGINS -->
  <div>
    <h4 class="lighter">
      <i class="ace-icon fa fa-hand-o-right"/>
      <router-link to="/business/course" class="pink">{{course.name}}</router-link>
    </h4>
    <hr>
    <p>
      <router-link to="/business/course" class="btn btn-white btn-warning btn-round">
        <i class="ace-icon fa fa-arrow-left bigger-120 blue"></i>
        大章
      </router-link>&nbsp;&nbsp;
      <button v-on:click="add()" class="btn btn-white btn-warning btn-round">
        <i class="ace-icon fa fa-edit bigger-120 blue"></i>
        新增
      </button>&nbsp;&nbsp;
      <button v-on:click="list(1)" class="btn btn-white btn-warning btn-round">
        <i class="ace-icon fa fa-refresh bigger-120 blue"></i>
        刷新
      </button>&nbsp;&nbsp;
    </p>

    <pagination ref="pagination" v-bind:list="list"></pagination>

    <table id="simple-table" class="table  table-bordered table-hover">
      <thead>
      <tr>
        <th>ID</th>
        <th>名称</th>
        <th>课程ID</th>
        <th>操作</th>
      </tr>
      </thead>

      <tbody>

      <tr v-for="chapter in chapters" :key="chapter.num">

        <td> {{chapter.id}} </td>
        <td> {{chapter.name}} </td>
        <td> {{chapter.courseId}} </td>

        <td>
          <div class="hidden-sm hidden-xs btn-group">
            <button v-on:click="toSection(chapter)" class="btn btn-xs btn-info">
              <i class="ace-icon fa fa-table bigger-120"></i>小节
            </button>
            &nbsp;&nbsp;
            <button v-on:click="edit(chapter)" class="btn btn-xs btn-info">
              <i class="ace-icon fa fa-pencil bigger-120"></i>编辑
            </button>
            &nbsp;&nbsp;
            <button v-on:click="del(chapter.id)" class="btn btn-xs btn-danger">
              <i class="ace-icon fa fa-trash-o bigger-120"></i>删除
            </button>
          </div>

          <div class="hidden-md hidden-lg">
            <div class="inline pos-rel">
              <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
              </button>

              <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                <li>
                  <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
                                  <span class="blue">
                                    <i class="ace-icon fa fa-search-plus bigger-120"></i>
                                  </span>
                  </a>
                </li>

                <li>
                  <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
                                  <span class="green">
                                    <i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
                                  </span>
                  </a>
                </li>

                <li>
                  <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
                                  <span class="red">
                                    <i class="ace-icon fa fa-trash-o bigger-120"></i>
                                  </span>
                  </a>
                </li>
              </ul>
            </div>
          </div>
        </td>
      </tr>

      </tbody>
    </table>

    <!--模态框 代码模板在bootstrap官网案例-->
    <!-- Modal -->
    <div class="modal fade" id="myFormModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">表单</h4>
          </div>
          <div class="modal-body">
            <!--表格-->
            <form class="form-horizontal">
              <!--1-->
              <div class="form-group">
                <label class="col-sm-2 control-label">名称</label>
                <div class="col-sm-10">
                  <input v-model="chapter.name" class="form-control" placeholder="输入名称"/>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">课程</label>
                <div class="col-sm-10">
                  <p class="form-control-static" >{{course.name}}</p>
                </div>
              </div>

            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" v-on:click="save()">保存</button>
          </div>
        </div><!-- /.modal-content-->
      </div><!-- /.modal-dialog-->
    </div><!-- /.modal-->

  </div>

</template>

<script>
import Pagination from "../../components/paginations";

export default {
  name: 'chapter',
  components: {Pagination},
  data: function (){
    return{
      //映射表单数据
      chapter: {},
      chapters: [],
      course:{},
    }
  },
  mounted:function (){
    let _this = this;
    _this.$parent.activeSidebar("business-course-sidebar");
    let course = SessionStorage.get(SESSION_KEY_COURSE) || {};
    if(Tool.isEmpty(course)){
      _this.$router.push("/welcome");
    }
    _this.course = course;
    _this.list(1);
  },
  methods:{
    add() {
      let _this = this;
      _this.chapter = {};
      $("#myFormModal").modal("show");
    },

    edit(chapter) {
      let _this = this;
      _this.chapter = $.extend({},chapter);
      $("#myFormModal").modal("show");
    },

    list(page){
      let _this = this;
      Loading.show();
      _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/chapter/list',{
        page:page,
        size:_this.$refs.pagination.size,
        courseId:_this.course.id,
      }).then((response)=>{
        Loading.hide();
        console.log("查询大章列表结果：",response);
        let resp = response.data;
        _this.chapters = resp.content.list;
        _this.$refs.pagination.render(page,resp.content.total);
      })
    },

    save(){
      let _this = this;

      //保存校验
      if(!Validator.require(_this.chapter.name,"名称")
          ||!Validator.length(_this.chapter.courseId,"课程ID",1,8)) {
        return;
      }

      _this.chapter.courseId = _this.course.id;

      _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/chapter/save',_this.chapter)
      .then((response)=>{
        console.log("保存大章列表结果：",response);
        let resp = response.data;
        if(resp.success){
          $("#myFormModal").modal("hide");
          _this.list(1);
          toast.success("保存成功！");
        }else{
          toast.warning(resp.message);
        }


      })
    },

    del(id){
      let _this = this;
      //弹出框
      Confirm.show("删除大章后不可恢复，您是否检查删除!",function (){
        _this.$ajax.delete(process.env.VUE_APP_SERVER + '/business/admin/chapter/delete/'+ id )
            .then((response)=>{
              console.log("删除大章列表结果：",response);
              let resp = response.data;
              if(resp.success){
                _this.list(1);
                toast.success("删除成功！");
              }
            });
      })


      // Swal.fire({
      //   title: '确认删除?',
      //   text:
      //   type: 'warning',
      //   showCancelButton: true,
      //   confirmButtonColor: '#3085d6',
      //   cancelButtonColor: '#d33',
      //   confirmButtonText: '确认!'
      // }).then((result) => {
      //   if (result.value) {
      //     _this.$ajax.delete('http://127.0.0.1:9002/business/admin/chapter/delete/'+ id )
      //         .then((response)=>{
      //           console.log("删除大章列表结果：",response);
      //           let resp = response.data;
      //           if(resp.success){
      //             _this.list(1);
      //             toast.success("删除成功！");
      //           }
      //         })
      //   }
      // })


    },
    /**跳转到小节*/
    toSection(chapter){
      let _this = this;
      SessionStorage.set(SESSION_KEY_CHAPTER, chapter);
      _this.$router.push("/business/section")
    },
  }
}
</script>